Why another JS util library?
1) 101 will be maintained to minimize overlap with vanilla JS.
- 101 utils are made to work well with vanilla JS methods.
- 101 will only duplicate vanilla JS to provide Functional Programming paradigms, or if
the method is not available in a widely supported JS version (currently ES5).
- Other libraries often duplicate a lot of ES5: forEach, map, reduce, filter, sort, and more.
2) No need for custom builds.
- With 101, import naturally, and what you use will be bundled.
- Each util method is a module that can be required
require('101/<util>')
. - Currently CommonJS (node, browserify, webpack, etc) is supported, I will add other module system support on request.
- Other libraries can be large, and require manually creating custom builds when optimizing for size.
Why not release each as individual modules?
I usually agree with this philosophy; however, while in practice, adherence to the module-pattern
can become quite annoying for micro-modules (like those in 101):
- Micro-modules existance throughout a project can change very frequently, because of this one may find
themselves constantly updating their package.json (repeatedly adding and removing the same micro-modules).
- Unbundling micro-modules can lead to projects with hundreds of dependencies which can be tedious to maintain.
Installation
npm install 101
Usage
assign (aka extend)
Just like ES6's Object.assign
. Extend an object with any number of objects (returns original).
var assign = require('101/assign');
var target = { foo: 1 };
var source1 = { bar: 1 };
var source2 = { baz: 1 };
assign(target, source1)
assign(target, source1, source2)
and
Functional version of &&
. Works great with array.reduce
.
var and = require('101/and');
and(true, false);
and(true, true);
and(true, "foo");
apply
Functional version of function.apply
.
Supports partial functionality (great with array functions).
var apply = require('101/apply');
[sum].map(apply(null, [1, 2, 3]));
function sum () { }
apply({ prop: 'val' })(function () { return this.prop; });
bindAll
Bind methods in an object.
You can pass an array containing the name of the methods to bind as second
argument or leave it empty to bind all the available methods.
var bindAll = require('101/bind-all');
var obj = {
init: function() {
this.on(this.handler);
},
on: function(handler) {
return handler();
},
handler: function() {
console.log(this.msg);
},
msg: 'Hello World'
}
obj.init();
bindAll(obj);
obj.init();
bindAll(obj, ['handler']);
obj.init();
clone
It's clone (Only exporting this bc it is used internal to 101)
var clone = require('101/clone');
var obj = {
foo: 1,
bar: 2
};
clone(obj);
compose
Functional composition method. Works great with array.reduce
.
var compose = require('101/compose');
compose(isNaN, parseInt)('nope');
converge
Converges an array of functions into one. Works great with compose
.
var converge = require('101/converge');
converge(mul, [add, sub])(6, 2);
[ {a: true, b: false}
, {a: false, b: false}
, {a: true, b: true}
].filter(converge(and , [pluck("a") , pluck("b")]));
[f, converge(g, [h, i]), j].reduce(compose);
curry
Returns a curried function.
var curry = require('101/curry');
function add(a, b) { return a + b; }
var curriedAdd = curry(add);
var add2 = curriedAdd(2);
add2(6);
add2(8);
function join() { return Array.prototype.slice.call(arguments).join(''); }
curry(join, 3)(1)(0)(1);
defaults
Fill non-existing object values with defaults. Use it to set defaults on options. Works with
supplying default values in sub-objects as well. Supports partial functionality (great with array
functions). Mutates first argument and returns mutated argument.
var defaults = require('101/defaults');
var opts = { foo: 0, bar: 1 };
var defs = { foo: 1, bar: 2, qux: 2 };
defaults(opts, defs);
[opts].map(defaults(defs));
var opts = {
foo: {
one: 1,
two: 2
}
};
var defs = {
foo: {
two: 20,
three: 30
}
};
defaults(opts, defs);
del
Functional version of delete obj[key] which returns the same obj without the deleted key.
Supports partial functionality (great with array functions, like map).
var del = require('101/del');
var obj = {
foo: 1,
bar: 2
};
del(obj, 'foo');
[obj, obj, obj].map(del('foo'));
var obj = {
foo: {
moo: 1,
boo: 2
},
bar: 3
};
del(obj, 'foo.moo');
del(obj, ['foo.moo', 'bar'])
envIs
Functional version of str === process.env.NODE_ENV
.
Or's multiple environments.
var envIs = require('101/env-is');
envIs('development');
envIs('production');
envIs('staging', 'production');
envIs('development', 'production');
equals
Functional implementation of Object.is with polyfill for browsers without implementations of Object.is
Supports partial functionality (great with array functions).
var equals = require('101/equals');
equals(1, 1);
[1,2,3].some(equals(1));
equals(1, '1');
exists
Simple exists function.
var exists = require('101/exists');
exists('foo');
exists(null);
exists(undefined);
find
Just like ES6's array.find
.
Finds the first value in the list that passes the given function (predicate) and returns it.
If list is not provided find will return a partial-function which accepts a list as the first argument.
var find = require('101/find');
var hasProps = require('101/has-properties');
var arr = [{ a: 1, b: 1 }, { b: 1 }, { c: 1 }];
var item = find(arr, hasProps({ a:1 }));
var partial = find(hasProps({ a: 1 }));
var item = partial(arr);
findIndex
Just like ES6's array.findIndex
.
Finds the first value in the list that passes the given function (predicate) and returns it's index.
If list is not provided findIndex will return a partial-function which accepts a list as the first argument.
var findIndex = require('101/find-index');
var arr = [1, 2, 3];
var index = findIndex(arr, function (val, i, arr) {
return val === 2;
});
flip
Returns a function with flipped arguments
var flip = require('101/flip');
var curry = require('101/curry');
var hasKeypaths = require('101/has-keypaths');
var hasFooBar = curry(flip(hasKeypaths))(['foo.bar']);
hasFooBar({ foo: { bar : true } });
function prefix(pre, str) {
return pre + str;
}
flip(prefix)('hello', '_');
groupBy
Hashes an array into groups based on the value of a provided common key.
Works nicely with pluck
and reduce
.
var groupBy = require('101/group-by');
var arr = [
{id: 1, foo: 'bar'},
{id: 2, foo: 'qux'},
{id: 3, foo: 'qux'}
];
groupBy(arr, 'foo')
arr.reduce(groupBy('foo'), {})
arr.reduce(groupBy(pluck('foo')), {})
hasKeypaths
Determines whether the keypaths exist and have the specified values.
Supports partial functionality (great with array functions, and 101/find).
var hasKeypaths = require('101/has-keypaths');
var obj = {
foo: {
bar: {
qux: 1
}
}
};
hasKeypaths(obj, ['foo.bar.qux']);
hasKeypaths(obj, { 'foo.bar.qux': 1 });
hasKeypaths(obj, ['foo.qux']);
hasKeypaths(obj, { 'foo.bar': 2 });
hasKeypaths(obj, { 'foo.bar': 1, 'nope': 1 });
var barObj = { bar: 1 };
hasKeypaths(obj, { 'foo.bar': barObj });
hasKeypaths(obj, { 'foo.bar': barObj }, true);
hasKeypaths(obj, { 'foo.bar': barObj }, false);
hasKeypaths(obj, { 'foo.bar': obj.foo }, false);
hasKeypaths(obj, ['foo.bar'], false);
var arr = [obj, { b: 1 }, { c: 1 }];
find(arr, hasKeypaths({ 'foo.bar.qux':1 }));
find(arr, hasKeypaths(['foo.bar.qux']));
var opts = {
host: 'localhost',
port: '3333',
user: {
id: 5
}
};
hasKeypaths(opts, ['host', 'port', 'user.id']);
hasProperties
Determines whether the keys exist and, if specified, has the values.
Supports partial functionality (great with array functions, and 101/find).
NOTE: I am considering deprecating this method, bc it is so similar to has-keypaths.
var hasProps = require('101/has-properties');
var obj = {
qux: 1
};
obj['foo.bar'] = 1
hasProps(obj, ['foo', 'qux']);
hasProps(obj, { qux: 1 })
var barObj = { bar: 1 };
hasProps(obj, { 'foo.bar': barObj });
hasProps(obj, { 'foo.bar': barObj }, true);
hasProps(obj, { 'foo.bar': barObj }, false);
hasProps(obj, ['foo.bar'], false);
var arr = [{ a: 1, b: 1 }, { b: 1 }, { c: 1 }];
find(arr, hasProps({ a:1 }));
find(arr, hasProps(['a']));
includes
Polyfill of ES7 proposed Array.prototype.includes. Will default to Array.prototype.includes if
present.
var includes = require('101/includes');
var haystack = ['a', 'b', 'c', 'd', 'e'];
includes(haystack, 'c');
includes(haystack, 'c', 3);
includes(haystack, 'c', 0);
var i = includes(haystack);
i('c')
i('g')
var not = require('101/not');
var notIn = not(includes);
[1, 2, 3, 4, 5].filter(notIn([1, 2, 3]));
indexBy
Hashes an array of objects based on the value of a provided common key.
Works nicely with pluck
and reduce
.
var arr = [
{foo: 'bar'},
{foo: 'qux'}
];
arr.reduce(indexBy('foo'), {})
arr.reduce(indexBy(pluck('foo')), {})
arr.reduce(indexBy(pluck('foo')), {})
instanceOf
Functional version of JavaScript's instanceof.
Supports partial functionality (great with array functions).
var instanceOf = require('101/instance-of');
['foo', 'bar', 1].map(instanceOf('string'));
isBoolean
Functional version of typeof val === 'boolean'
.
Supports partial functionality (great with array functions).
var isBoolean = require('101/is-boolean');
[true, false, 1].map(isBoolean);
isEmpty
Functional version of val empty object, array or object
var isEmpty = require('101/is-empty');
isEmpty([]);
isEmpty({});
isEmpty("");
isEmpty(" ");
isFunction
Functional version of typeof val === 'function'
var isFunction = require('101/is-function');
[parseInt, function () {}, 'foo'].map(isFunction);
isInteger
Check if a value is an instance of an integer.
var isInteger = require('101/is-Integer');
isInteger(101);
isInteger(101.01);
isNumber
Functional version of val typeof 'number'.
var isNumber = require('101/is-number');
['foo', NaN, 1].map(isNumber);
isObject
Functional strict version of val typeof 'object' (and not array or regexp)
var isObject = require('101/is-object');
[{}, { foo: 1 }, 100].map(isObject);
isRegExp
Check if a value is an instance of RegExp
var isRegExp = require('101/is-regexp');
[new RegExp('.*'), /.*/, {}, 1].map(isRegExp);
isString
Functional version of val typeof 'string'
var isString = require('101/is-string');
['foo', 'bar', 1].map(isString);
keysIn
Return an array containing all the keys of an object.
It differs from the native Object.keys
by including also the prototype
keys.
var keysIn = require('101/keys-in');
var User = function() {
this.msg = 'Hello World';
}
User.prototype.isLoggedIn = function() { }
var user = new User();
keysIn(user);
last
Returns the last value of a list
var last = require('101/last');
last([1, 2, 3]);
last('hello');
lens
Create a lens to access a data structure. When passed a property key as a string, it returns a function fn(obj)
that acts as a getter for that. It also exposes .set(value, obj)
and .mod(fn, obj)
.
var fooLens = lens('foo');
var toUpper = function(str) { return str.toUpperCase(); };
var obj = {
foo: 'foo',
bar: 'bar'
};
fooLens(obj);
fooLens.set('moo', obj);
fooLens.mod(toUpper, obj);
You may also provide getter and setter functions.
var arr = ['foo', 'bar'];
var first = lens(
function(arr) { return arr[0]; },
function(val, arr) { var clone = arr.slice(); clone[0] = val; return clone; }
);
first(arr);
first.set('moo')(arr);
first.mod(toUpper)(arr);
noop
No-op function
require('101/noop');
not
Functional version of !
.
var not = require('101/not');
not(isString)('hey');
not(isString)(100);
omit
Immutable version of delete obj.key
. Returns a new object without the specified keys.
Supports partial functionality (great with array functions, like map).
var omit = require('101/omit');
var obj = {
foo: 1,
bar: 2
};
omit(obj, 'foo');
omit(obj, ['foo']);
omit(obj, ['foo', 'bar']);
[obj, obj, obj].map(omit('foo'));
or
Functional version of ||
.
Works great with array.reduce
.
var or = require('101/or');
or(true, true);
or(true, false);
or(false, false);
or("foo", false);
passAll
Muxes arguments across many functions and &&
's the results.
Supports partial functionality (great with array functions, like map).
var passAll = require('101/pass-all');
['', 'foo', 'bar', 100].map(passAll(isString, isTruthy));
passAny
Muxes arguments across many functions and ||
's the results.
Supports partial functionality (great with array functions, like map).
var passAny = require('101/pass-any');
['', 'foo', 'bar', 100].map(passAny(isString, isNumber));
pick
Returns a new object with the specified keys (with key values from obj).
Supports regular expressions and partial functionality (great with array functions, like map).
var pick = require('101/pick');
var obj = {
foo: 1,
bar: 2,
qwk: {
wrk: 1
},
'qwk.wrk': 2
};
pick(obj, 'foo');
pick(obj, RegExp('oo$'));
pick(obj, ['foo']);
pick(obj, ['foo', 'bar']);
[obj, obj, obj].map(pick('foo'));
pick(obj, 'qwk.wrk');
pick(obj, '["qwk.wrk"]');
pluck
Functional version of obj[key], returns the value of the key from obj.
Supports partial functionality (great with array functions, like map).
var pluck = require('101/pluck');
var obj = {
foo: 1,
bar: 2
};
pluck(obj, 'foo');
[obj, obj, obj].map(pluck('foo'));
var obj = {
foo: {
bar: 1
},
'foo.bar': 2
};
pluck(obj, 'foo.bar');
pluck(obj, 'foo.bar', false);
put
Immutable version of obj[key] = val
. Returns a clone of the obj with the value put at the key.
Supports partial functionality (great with array functions, like map).
var put = require('101/put');
var obj = {
foo: 1,
bar: 2
};
put(obj, 'baz', 3);
obj;
[obj, obj, obj].map(put('foo', 100));
obj;
var obj = {
bar: 2
};
put(obj, 'foo.qux', 100);
put(obj, {
'foo.qux': 100
'yolo': 1
});
obj;
set
Functional version of obj[key] = val, returns the same obj with the key and value set.
Supports partial functionality (great with array functions, like map).
var set = require('101/set');
var obj = {
foo: 1,
bar: 2
};
set(obj, 'foo');
[obj, obj, obj].map(set('foo', 100));
var obj = {
bar: 2
};
set(obj, 'foo.qux', 100);
set(obj, {
'foo.qux': 100
'yolo': 1
});
values
Returns Array containing the values of the properties of an object
var values = require('101/values');
var obj {
foo: 'apple',
bar: 'orange'
};
var objValues = values(obj);
objValues
xor
Exclusive or
Works great with array.reduce
.
var xor = require('101/xor');
xor(true, true);
xor(true, false);
xor(false, true);
xor(false, false);
License
MIT